package com.schoolvisitors.vo;

import com.alibaba.fastjson2.JSONArray;
import com.schoolvisitors.entity.Reservation;
import com.schoolvisitors.entity.TimeSlot;
import com.schoolvisitors.entity.User;
import com.schoolvisitors.enums.ReservationStatusEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TimeSlotVo {
    Long id;
    String text;
    boolean isOccupied;

    public static JSONArray reservationToVos(Long userId, List<TimeSlot> timeSlots, List<Reservation> reservations) {
        List<Long> occupiedIdList = new ArrayList<>();

        for (Reservation cubicleReservation : reservations) {
            // 只有审核通过和未取消的才显示占用
            if (cubicleReservation.getStatus() == ReservationStatusEnum.APPROVED) {
                occupiedIdList.add(cubicleReservation.getTimeSlot().getId());
                // 如果是当前用户申请也显示已占用
            } else if (cubicleReservation.getStatus() == ReservationStatusEnum.PENDING && cubicleReservation.getUser().getId().equals(userId)) {
                occupiedIdList.add(cubicleReservation.getTimeSlot().getId());
            }
        }

        JSONArray jsonArray = new JSONArray();
        //{ id: 1, text: "06:00 - 12:00", isOccupied: false, isSelected: false },
        for (TimeSlot timeSlot : timeSlots) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", timeSlot.getId());
            map.put("text", timeSlot.getStartTime() + "-" + timeSlot.getEndTime());
            map.put("isOccupied", occupiedIdList.contains(timeSlot.getId()));
            jsonArray.add(map);
        }
        return jsonArray;
    }

}
